<template>
  <a-form class="smart-query-form" v-privilege="'oa:enterprise:query'">
    <a-row class="smart-query-form-row">
      <a-form-item label="登录名" class="smart-query-form-item">
        <a-input style="width: 300px" v-model:value="queryForm.username" />
      </a-form-item>
      <a-form-item label="姓名" class="smart-query-form-item">
        <a-input style="width: 300px" v-model:value="queryForm.displayName" />
      </a-form-item>
      <a-form-item label="员工编号" class="smart-query-form-item">
        <a-input style="width: 300px" v-model:value="queryForm.employeeNumber" />
      </a-form-item>
      <a-form-item label="开始时间" class="smart-query-form-item">
        <a-date-picker style="width: 300px" v-model:value="queryForm.startDate" valueFormat="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item label="结束时间" class="smart-query-form-item">
        <a-date-picker style="width: 300px" v-model:value="queryForm.endDate" valueFormat="YYYY-MM-DD" />
      </a-form-item>

      <a-form-item class="smart-query-form-item smart-margin-left10">
        <a-button-group>
          <a-button type="primary" @click="onSearch">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="resetQuery">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-button-group>
      </a-form-item>
    </a-row>
  </a-form>

  <a-card size="small" :bordered="false" :hoverable="true">
    <a-table size="small" rowKey="id" :dataSource="tableData" :columns="columns" :pagination="false"
      :loading="tableLoading" bordered>
      <template #bodyCell="{ column, record, text, index }">
        <template v-if="column.dataIndex === 'index'">
          {{ index+1 }}
        </template>
      </template>
    </a-table>

    <div class="smart-query-table-page">
      <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize" v-model:current="queryForm.pageNumber"
        v-model:pageSize="queryForm.pageSize" :total="total" @change="ajaxQuery" @showSizeChange="ajaxQuery"
        :show-total="(total) => `共${total}条`" />
    </div>
  </a-card>
</template>
<script setup>
import { reactive, ref, onMounted } from 'vue';
import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
import { smartSentry } from '/@/lib/smart-sentry';
import { logAuditApi } from '/@/api/system/logAudit-api'


// --------------------------- 企业表格 列 ---------------------------
const columns = ref([
  {
    title: '序号',
    dataIndex: 'index',
    width: 50,
    align: 'center'
  },
  {
    title: '会话标识',
    dataIndex: 'sessionId',
  },
  {
    title: '登录名',
    dataIndex: 'username',
  },
  {
    title: '状态',
    dataIndex: 'message',
  },
  {
    title: '访问地址',
    dataIndex: 'sourceIp',
  },
  {
    title: '归属地',
    dataIndex: 'location',
  },
  {
    title: '浏览器',
    dataIndex: 'browser',
  },
  {
    title: '平台',
    dataIndex: 'platform',
  },
  {
    title: '登录时间',
    dataIndex: 'loginTime',
  },
  {
    title: '退出时间',
    dataIndex: 'logoutTime',
  },
]);

// --------------------------- 查询 ---------------------------
const queryFormState = {
  pageNumber: 1,
  pageSize: PAGE_SIZE,
  searchCount: true,
  username: '',
  displayName: '',
  employeeNumber: '',
  startDate: '',
  endDate: '',
  // startDatePicker:'',
  // endDatePicker:'',
};
const queryForm = reactive({ ...queryFormState });
const tableLoading = ref(false);
const tableData = ref([]);
const total = ref(0);


// 查询
function onSearch() {
  queryForm.pageNumber = 1;
  ajaxQuery();
}
// 重置查询
function resetQuery() {
  Object.assign(queryForm, queryFormState);
  ajaxQuery();
}
// 列表数据获取
async function ajaxQuery() {
  try {
    tableLoading.value = true;
    let responseModel = await logAuditApi.getLoginHistory(queryForm);
    const list = responseModel.data.rows
    total.value = responseModel.data.records;
    tableData.value = list;
  } catch (e) {
    smartSentry.captureError(e);
  } finally {
    tableLoading.value = false;
  }
}
onMounted(() => {
  ajaxQuery();
});
</script>